package com.yuexun.dhdoctor.module.my.ui;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.base.BaseKJActivity;
import com.yuexun.dhdoctor.config.AppConfig;
import com.yuexun.dhdoctor.module.login.bean.LoginBean;
import com.yuexun.dhdoctor.module.login.bean.UserBean;
import com.yuexun.dhdoctor.module.login.model.impl.LoginModel;
import com.yuexun.dhdoctor.module.login.presenter.impl.LoginPresneter;
import com.yuexun.dhdoctor.module.login.ui.view.ILoginView;
import com.yuexun.dhdoctor.util.FileUtils;
import com.yuexun.dhdoctor.widget.HttpDownloader;

import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/4/26 0026.
 */

public class Act_Sign extends BaseKJActivity implements ILoginView {
    @Bind(R.id.qrcode_view)
    ImageView qrcodeView;
    @Bind(R.id.title_tv)
    TextView titleTv;
    @Bind(R.id.txt_view)
    TextView txtView;

    String signUrl;
    final FileUtils fileUtils = new FileUtils(this);

    private LoginPresneter presenter;

    @Override
    public void setRootView() {
        setContentView(R.layout.act_qrcode);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        titleTv.setText("我的签名");
        txtView.setText("没有签名");
        super.initData();
        presenter = new LoginPresneter(new LoginModel(), this);
        Map<String, String> params = new HashMap<>();
        params.put("sessionUuid", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
        presenter.getUseInfo(params);
    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            switch (msg.what) {
                case 0:
                    ViewInject.toast("开始下载...");
                    break;
                case 1:
                    ViewInject.toast("下载完成");
                    showSign();
                    break;
                case 2:
                    ViewInject.toast("下载失败");
                    break;
                case 3:
                    showSign();
                    break;
            }
        }

    };

    private void showSign() {
        txtView.setVisibility(View.GONE);
        String s = Environment.getExternalStorageDirectory() + "/" + AppConfig.FILE_CONTENT + signUrl + ".png";
        try {
            FileInputStream fis = new FileInputStream(s);
            Bitmap bmp = BitmapFactory.decodeStream(fis);
            qrcodeView.setImageBitmap(bmp);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    @OnClick(R.id.back)
    public void onClick() {
        finish();
    }

    @Override
    public void loadingDialogStatus(int status) {

    }

    @Override
    public void doInLogin(LoginBean loginBean) {

    }

    @Override
    public void getUserInfo(UserBean userBean) {
        String filename = AppConfig.USER_INFO;
        PreferenceHelper.write(aty, filename, AppConfig.SIGN_URL, userBean.getDatas().get(0).getSignUrl());

        if (userBean.getDatas().get(0).getSignUrl() != null &&
                !userBean.getDatas().get(0).getSignUrl().isEmpty()) {
            signUrl = userBean.getDatas().get(0).getSignUrl();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    if (!fileUtils.isFileExist(AppConfig.FILE_CONTENT + signUrl + ".png")) {
                        HttpDownloader httpDownLoader = new HttpDownloader(aty);
                        handler.sendEmptyMessage(0);
                        int result = httpDownLoader.downfile(AppConfig.WEB_FILE + signUrl,
                                AppConfig.FILE_CONTENT, signUrl + ".png");
                        if (result == 0) {
                            handler.sendEmptyMessage(1);
                        } else if (result == 1) {

                        } else if (result == -1) {
                            handler.sendEmptyMessage(2);
                        }
                    } else {
                        handler.sendEmptyMessage(3);
                    }
                }
            }).start();
        } else {
            qrcodeView.setVisibility(View.GONE);
            txtView.setVisibility(View.VISIBLE);
            txtView.setText("没有签名");
        }
    }
}
